package com.ftg.learn.springboot.demo.controller;


import com.ftg.learn.springboot.demo.pojo.apply;
import com.ftg.learn.springboot.demo.service.applyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("apply")
public class applyController {

	@Autowired
	applyService as;

	@PostMapping("/add")
	public int add(apply apply){

		return as.add(apply);
	}
	@PostMapping("/upload")
	public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {

		Map<String, Object> map = new HashMap<>();

		if (file.isEmpty()) {
			map.put("code",1);
			map.put("msg","上传失败，请重新选择文件");
			return map;
		}

		String fileName = file.getOriginalFilename();
		String filePath = "D://elvis_img/";
		//System.out.println("fileName"+fileName);
		String nginxPath = "http://192.168.1.123:8081/" + fileName;
		File dest = new File(filePath + fileName);
		try {
			file.transferTo(dest);

			//as.saveFilePathToApply(nginxPath);
			map.put("alogo", nginxPath);
			map.put("code", 0);
			map.put("msg","上传成功");
			return map;
		} catch (IOException e) {
			e.printStackTrace();

			map.put("code",1);
			map.put("msg","上传失败！");
		}
		return map;
	}

}
